package info.niwota.group;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;

/**
 * @author qiangli
 * 
 */
public final class Files {
	// private static final Properties contentTypes = new Properties();

	// static String replaceBlank(String s, String c) {
	// return s.replaceAll("\\s+", c);
	// }

	public static final String SUB_IMAGE = "images";
	public static final String SUB_AUDIO =  "audio";
	
	private static final String TOP = "ethread";

	public static File getTempFile(File dir, String prefix, String ext)
			throws IOException {
		File eroot = ensureRoot(dir);
		return File.createTempFile(prefix, ext, eroot);
	}

	public static File getSubFolder(Context ctx, String sub) {
		String path = Preferences.getEtalkFolder(ctx, null);
		if (path == null) {
			path = Files.getDefaultRoot().getPath();
		}
		File dir = new File(path, sub);
		return dir;
	}

	public static File getDefaultRoot() {
		File root = Environment.getExternalStorageDirectory();
		File sub = new File(root, TOP);
		return sub;
	}

	private static File ensureRoot(File dir) {
		// specified default dir
		// or default root /sdcard/etalk
		// or /sdcard/sdcard
		File root = dir;
		if (!root.exists()) {
			if (!root.mkdirs()) {
				root = null;
			}
		}
		//valid or use default
		if (root == null || !root.isDirectory() || !root.canWrite()) {
			// try our default directory
			File sub = getDefaultRoot();
			if (!sub.exists()) {
				if (sub.mkdirs()) {
					root = sub;
				}
			} else {
				if (sub.isDirectory()) {
					root = sub;
				}
			}
		}
		//
		if (root == null || !root.canWrite()) {
			// Workaround for broken sdcard support on the device.
			root = new File("/sdcard/sdcard");
		}
		return root;
	}

	public static File toFile(String uri) {
		if (uri.startsWith("file:")) {
			Uri u = Uri.parse(uri);
			String p = u.getPath();
			return new File(p);
		}
		return new File(uri);
	}

	public static Uri toUri(String path) {
		if (path.startsWith("file:")) {
			return Uri.parse(path);
		}
		return Uri.parse("file:" + path);
	}

	//	
	// private void loadContentTypes(String file) {
	// try {
	// File f = new File(file);
	// if (f.exists()) {
	// InputStream is = f.getInputStream();
	// contentTypes.load(is);
	// is.close();
	// }
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	//
	// public static String guessType(String name, String def) {
	// String type = "application/octet-stream";
	// String ext = fileExt(name);
	// if (ext == null) {
	// return type;
	// }
	// if (contentTypes != null) {
	// type = contentTypes.getProperty(ext, type);
	// }
	// return type;
	// }

	public static String guessType(String path) {
		String ext = fileExt(path);
		if (ext == null) {
			return null;
		}
		//
		final String[] imageExts = new String[] { "jpg", "gif", "png", "bmp" };
		final String[] imageTypes = new String[] { "image/jpeg", "image/gif",
				"image/png", "image/bmp"};
		int len = imageExts.length;
		for (int i = 0; i < len; i++) {
			if (imageExts[i].equals(ext)) {
				return imageTypes[i];
			}
		}
		//
		final String[] videoExts = new String[] { "3gp", "mp4", "wmv" };
		final String[] videoTypes = new String[] { "video/3gpp", "video/mpeg",
				"video/*"};
		len = videoExts.length;
		for (int i = 0; i < len; i++) {
			if (videoExts[i].equals(ext)) {
				return videoTypes[i];
			}
		}
		return null;
	}

	public static String fileExt(String name) {
		int idx = name.lastIndexOf(".");
		if (idx > 0) {
			return name.substring(idx + 1).toLowerCase();
		}
		return null;
	}
}
